package com.tpsix.datascope;

import cn.hutool.core.collection.CollUtil;
import com.tpsix.controller.vm.RoleVM;
import com.tpsix.domain.SysDept;
import com.tpsix.enums.DataScopeTypeEnum;
import com.tpsix.service.SysDeptService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 自定义数据范围
 */
@Component
@AllArgsConstructor
public class CustomDataScopeHandler implements DataScopeHandler {

    private final SysDeptService sysDeptService;

    @Override
    public List<Integer> getDeptIds(RoleVM roleVM, DataScopeTypeEnum dataScopeTypeEnum) {
        // 自定义部门id
        List<Integer> roleDeptIds = roleVM.getRoleDeptIds();

        if (CollUtil.isEmpty(roleDeptIds)) {
            return Collections.emptyList();
        }

        // 批量获取可用的部门id
        List<SysDept> dept = sysDeptService.lambdaQuery().select(SysDept::getId)
                .in(SysDept::getId, roleDeptIds).list();

        return dept.stream().map(SysDept::getId).collect(Collectors.toList());
    }
}
